package GCJ2009.Round1C;

import java.io.*;
import java.util.*;

public class All_Your_Base {

    BufferedReader fin;
    PrintWriter fout;

    void openFiles() throws IOException {
        fin = new BufferedReader(new FileReader("A-large-practice.in"));
        fout = new PrintWriter(new BufferedWriter(new FileWriter("A-large-practice.out")));
    }

    void closeFiles() throws IOException {
        fin.close();
        fout.close();
    }

    int d[], c[], res[], digit;

    void input() throws IOException {
        String s = fin.readLine();
        d = new int[s.length()];
        for (int i = 0; i < s.length(); i++)
            if (Character.isLowerCase(s.charAt(i)))
                d[i] = s.charAt(i) - 'a' + 10;
            else
                d[i] = s.charAt(i) - '0';
    }
    
    int nextDigit(int v)
    {
        if (v == 1) return 0;
        if (v == 0) return 2;
        return v+1;
    }

    void init()
    {
        digit = 1;
        
        c = new int[36];
        for (int i = 0; i < 36; i++) c[i] = -1;

        res = new int[d.length];
    }

    void process() {
        init();
        for (int i = 0; i < d.length; i++)
        {
            if (c[d[i]] == -1)
            {
                c[d[i]] = digit;
                res[i] = digit;
                digit = nextDigit(digit);
            }
            else res[i] = c[d[i]];
        }
    }

    void output() {
        int base;
        long result = 0;
        if (digit == 0) base = 2;
        else base = digit;

        for (int i = 0; i < res.length; i++)
            result = result * base + res[i];

        fout.println(result);
    }

    void takeTest() throws IOException
    {
        String s = fin.readLine();
        int ntest = Integer.parseInt(s);
        for (int i = 1; i <= ntest; i++)
        {
            input();
            process();
            fout.print("Case #" + i + ": ");
            output();
        }
    }

    public static void main(String[] args) throws IOException {
        All_Your_Base myProgram = new All_Your_Base();

        myProgram.openFiles();
        myProgram.takeTest();
        myProgram.closeFiles();
    }
}